/**
 * Copyright 2013 - Les petits mots
 */

/**
 * Variables globales
 */
var chosenLevel=null; 
var chosenWord=null; // contient l'id du mot
var chosenGame=null;
const LEVEL_1="1";
const LEVEL_2="2";
const LEVEL_3="3";
const GAME_1="game1";
const GAME_2="game2";
const VIBRATING_DURATION=500;

/**
 * Initialisation du jeu :
 *   - Génération de la liste des images
 *   - Pamamétrage du bouton quitter
 *   - Configuration du sélecteur de niveaux
 */
$(document).ready(function() {
	generateWordChooserPage();
	generateEventsWordChooserPage();
	
	// Génération du tableau des id des mots
	for(word in words) {
		idWords.push(word);
	}
	
	$("#btnLeave").click(function() {
		navigator.app.exitApp();
	});
	
	$("#playBtn").click(function() {
		chosenLevel = $("input[name=level]:checked").val();
		if(chosenLevel != null) {
			window.location.href="#" + chosenGame;
		}
		if(chosenGame == GAME_1){
			initGame(chosenWord, chosenLevel);
		} else if(chosenGame == GAME_2) {
			initPuzzleGame(chosenWord, chosenLevel);
		}
	});
	
	$("#game1, #game2").click(function() {
		chosenGame = $(this).attr("id");
		generateMedalsOfWordChooser();
	});
	
	
	$(".playSoundGame").click(function(){
		playSound();
	});
	
	$(".setNextGame").click(function(){
		var index = jQuery.inArray(chosenWord,idWords) + 1;
		if(index >= idWords.length){
			alert("Fin de la liste pour ce niveau.");
			window.location.href="index.html";
		}
		else {
			chosenWord = idWords[index];
			switch(chosenGame){
			case GAME_1:
				initGame(chosenWord, chosenLevel);
				break;
			case GAME_2:
				initPuzzleGame();
				break;
			}
			$(".popupMenuGames").popup();
			$(".popupMenuGames").popup("close");
		}
	});
	
	$("#btnProgress").click(function() {
		generateProgressPage();
	});
	
	// Config du BackButton
	document.addEventListener("deviceready", function() {
		document.addEventListener("backbutton", function(e) {
			if($.mobile.activePage.is('#home')){
		        e.preventDefault();
		        navigator.app.exitApp();
		    }
		    else {
		        navigator.app.backHistory()
		    }
		}, false);
	},false);
});

/**
 * 
 * @param game Jeu en cours
 * @returns
 */
function generateMedalsOfWordChooser() {
	for(word in words) {
		var html = "";
		
		if(getItemProgressionById(chosenGame,LEVEL_3,word)) {
			html += '<span class="goldMedal"></span>';
		}
		if(getItemProgressionById(chosenGame,LEVEL_2,word)) {
			html += '<span class="silverMedal"></span>';
		}
		if(getItemProgressionById(chosenGame,LEVEL_1,word)) {
			html += '<span class="bronzeMedal"></span>';
		}

		$("#medals_" + word).html(html);
	}
}

/**
 * Génère la page HTML de la page où l'on peut choisir le mot
 */
function generateWordChooserPage() {
	var html="";
	
	html = '<ul data-role="listview" data-inset="true">';
	for(word in words) {
		html += '<li><a href="#levelChooser" id="' + word + '">'
		html += '<img src="'+ words[word].image_src +'">';
		//html += '<h2>H2</h2>';
		html += '<p id="medals_' + word + '"></p>';
		//html += '<p class="ui-li-aside">titre p</p>';
		html += '</a></li>';	
	}
	html += '</ul>'
	
	$("#wordChooser [data-role=\"content\"]").append(html);
}

function generateEventsWordChooserPage() {
	for(word in words) {
		$("#" + word).click(function() {
			chosenWord = $(this).attr("id");
		});
	}
}
